﻿
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.UI.WebControls;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Event
{
    public partial class EventManagement : CGOBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    BindData(true);
                }
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            foreach (DataGridColumn column in grdEvent.Columns)
            {
                if (object.ReferenceEquals(column.GetType(), typeof(ImageCommandColumn)))
                {
                    ImageCommandColumn imageColumn = (ImageCommandColumn)column;                    
                    if (!String.IsNullOrEmpty(imageColumn.CommandName))
                    {
                        imageColumn.Text = Localization.GetString(imageColumn.CommandName, this.LocalResourceFile);
                    }
                    if (imageColumn.CommandName == "Edit")
                    {
                        string formatString = ModuleContext.EditUrl("EventID", "KEYFIELD", "Edit");
                        formatString = formatString.Replace("KEYFIELD", "{0}");
                        imageColumn.NavigateURLFormatString = formatString;
                    }
                }
            }
        }

        protected void cgoPaging_PageChangeEvent(object sender, EventArgs e)
        {
            BindData(false);
        }

        private void BindData(bool refreshPaging)
        {
            EventController ctrlEvent = new EventController();
            List<EventInfo> lstEvent;
            cgoPaging.Visible = true;
            lstEvent = ctrlEvent.GetEvents(PortalId, 0, Null.NullDate, Null.NullDate, 
                refreshPaging, cgoPaging.PageSize, cgoPaging.CurrentPage);
            if (refreshPaging && lstEvent.Count > 0 && lstEvent[0].TotalRowCount > cgoPaging.PageSize)
            {                
                cgoPaging.TotalCount = lstEvent[0].TotalRowCount;
            }

            if (cgoPaging.TotalCount < cgoPaging.PageSize)
            {
                cgoPaging.Visible = false;
            }

            grdEvent.DataSource = lstEvent;
            grdEvent.DataBind();
        }
    }
}